package com.qding.park.manager.service.queue.biz;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.qding.park.common.core.exceptions.business.adapter.AdapterException;
import com.qding.park.common.core.exceptions.business.manager.adapterfactory.AdapterProxyCreateFail;
import com.qding.park.common.core.exceptions.system.NetWorkException;
import com.qding.park.common.utils.OrderNumUtil;
import com.qding.park.manager.adapter.factory.AdapterParkProxyFactory;
import com.qding.park.manager.payment.PaymentInterfaceServiec;
import com.qding.park.manager.pojo.TbFeetrade;
import com.qding.park.manager.pojo.TbParkspace;
import com.qding.park.manager.pojo.TbReserve;
import com.qding.park.manager.pojo.enums.OrderServiceType;
import com.qding.park.manager.pojo.enums.RecordStatusEnum;
import com.qding.park.manager.pojo.enums.ReserverStatusEnum;
import com.qding.park.manager.service.queue.mapper.TbFeetradeMapper;
import com.qding.park.manager.service.queue.mapper.TbParkspaceMapper;
import com.qding.park.manager.service.queue.mapper.TbReserveMapper;
import com.qding.park.target.common.adapter.PayFeeAdapter;
import com.qding.park.target.common.entity.AdTempPayResult;

@Component("tempPayBiz")
public class TempPayBiz {
    
    @Autowired
    private TbParkspaceMapper tbParkspaceMapper;
    
    @Autowired
    private AdapterParkProxyFactory adapterParkProxyFactory;
    
    @Autowired
    private TbReserveMapper tbReserveMapper;
    
    @Autowired
    private TbFeetradeMapper tbFeetradeMapper;
    
    @Autowired
    private PaymentInterfaceServiec paymentInterfaceServiec;
    
    public void teneratPayed(TbReserve tbReserve) throws AdapterProxyCreateFail, NetWorkException, AdapterException {
        // 得到车场编号
        int parkId = tbReserve.getParkID();
        int userId = tbReserve.getUserID();
        // 车场订单号
        String parkOrderId = tbReserve.getParkOrderId();
        // 查询车场信息
        TbParkspace tbParkspace = tbParkspaceMapper.queryParkInfoByParkId(parkId);
        String parkCode = tbParkspace.getParkCode();
        int supplierId = tbParkspace.getSupplierId();
        AdTempPayResult apTempPayOrderTemp = new AdTempPayResult();
        apTempPayOrderTemp.setParkCode(parkCode);
        apTempPayOrderTemp.setOrderId(tbReserve.getParkOrderId());
        apTempPayOrderTemp.setRealAmount(tbReserve.getDeposit());
        apTempPayOrderTemp.setShouldAmount(tbReserve.getDeposit());
        apTempPayOrderTemp.setPayTime(new Date());
        PayFeeAdapter adapterProxy = adapterParkProxyFactory.getPayFeeAdapter(String.valueOf(supplierId));
        adapterProxy.tempPaySuccessNotice(apTempPayOrderTemp);
        tbReserveMapper.teneratParkHandled(tbReserve.getId());
    }
    
    
    public void tempPayed(TbReserve tbReserve) throws AdapterProxyCreateFail, NetWorkException, AdapterException {
        // 得到车场编号
        int parkId = tbReserve.getParkID();
        int userId = tbReserve.getUserID();
        // 车场订单号
        String parkOrderId = tbReserve.getParkOrderId();
        // 查询车场信息
        TbParkspace tbParkspace = tbParkspaceMapper.queryParkInfoByParkId(parkId);
        String parkCode = tbParkspace.getParkCode();
        int supplierId = tbParkspace.getSupplierId();
        AdTempPayResult apTempPayOrderTemp = new AdTempPayResult();
        apTempPayOrderTemp.setParkCode(parkCode);
        apTempPayOrderTemp.setOrderId(tbReserve.getParkOrderId());
//        apTempPayOrderTemp.setPayValue(tbReserve.getDeposit());
        apTempPayOrderTemp.setShouldAmount(tbReserve.getDeposit());
        apTempPayOrderTemp.setRealAmount(tbReserve.getDeposit());
        apTempPayOrderTemp.setPayTime(new Date());
        PayFeeAdapter adapterProxy = adapterParkProxyFactory.getPayFeeAdapter(String.valueOf(supplierId));
         adapterProxy.tempPaySuccessNotice(apTempPayOrderTemp);
        
        tbReserveMapper.tempParkHandled(tbReserve.getId());
    }
    
    @Transactional(rollbackFor = Throwable.class)
    public void tempIssuedFailRollBack(TbReserve tbReserve){
        tbReserve.setStatus(ReserverStatusEnum.TEMP_PAY_PARK_FAIL.getStatus());
        tbReserveMapper.tempIssuedFailRollBack(tbReserve);
        
        /**** *此处要调用北京接口 ********/
        // 退款押金
        TbFeetrade tbFeetrade = new TbFeetrade();
        tbFeetrade.setId(OrderNumUtil.getOrderNum(tbReserve.getUserID(),OrderServiceType.FEETRADE.getTag()));
        tbFeetrade.setUserID(tbReserve.getUserID());
        tbFeetrade.setParkID(tbReserve.getParkID());
        tbFeetrade.setFeeType(TbFeetrade.FeeTypeEnum.REFUND_PAY.getType());
        tbFeetrade.setFeeCost(tbReserve.getRealPayDeposit());
        tbFeetrade.setBusinessID(tbReserve.getId());
        tbFeetrade.setChannel(TbFeetrade.ChannelEnum.CAPITALFLOW_ONLINE.getType());
        tbFeetrade.setStatus(RecordStatusEnum.EFFICIENCY.getStatus());
        tbFeetradeMapper.save(tbFeetrade);
        /**** 调用北京接口 *****/
        // 没有超时 退还押金
        paymentInterfaceServiec.refundOrder(tbReserve.getBjOrderId());
        /**** 调用北京接口 *****/
    }

}
